/*
 *  Copyright (C) 2011 Hatsites
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.myorg.ctm.etk.gui.swg.util.property;

import java.io.File;
import org.myorg.shr.gui.swg.util.property.PrpMgrAbs;
import java.util.Properties;

/**
 *
 * @author Hatsites
 *
 * email: Hatsites_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 */

final public class PrpMgrAppli extends PrpMgrAbs {

    private PrpMgrAppli(){

        super(PrpMgrAppli._STR_NAMEFILE_);
    }

    public static void s_init() throws Exception{
        if (_INSTANCE_ == null) {
                _INSTANCE_ = new PrpMgrAppli();
        }
        PrpNamingEtk.s_init();
        try{
            PrpMgrAppli.read();
        }
        catch(Exception exc){
            PrpMgrAppli.write();

        }

        String str = _prp_.getProperty(PrpNamingEtk.STR_PRP_CONFIG_PERFORMED);

        if (str == null){
            PrpMgrAppli._prp_.setProperty(PrpNamingEtk.STR_PRP_CONFIG_PERFORMED, STR_CONFIG_FALSE);
            write();
        }

        return;
    }

    public static void s_destroy(){
        PrpNamingEtk.s_destroy();
        if (_INSTANCE_ == null || PrpMgrAppli._prp_ == null){
            return;
        }
        PrpMgrAppli._prp_.clear();
        PrpMgrAppli._prp_ = null;
        _INSTANCE_ = null;

    }

    public static void s_setConfigPerformed(boolean bln) throws Exception{

        if (bln){
            PrpMgrAppli._prp_.setProperty(PrpNamingEtk.STR_PRP_CONFIG_PERFORMED, STR_CONFIG_TRUE);
        }
        else{
            PrpMgrAppli._prp_.setProperty(PrpNamingEtk.STR_PRP_CONFIG_PERFORMED, STR_CONFIG_FALSE);
        }
        write();

    }

    public static boolean s_getConfigPerformed() throws Exception{

        read();
        if (_prp_.getProperty(PrpNamingEtk.STR_PRP_CONFIG_PERFORMED).compareTo(STR_CONFIG_TRUE) == 0){

            return true;
        }
        else{

            return false;
        }

    }

    static public String s_getPathAbsLastParentFolder() throws Exception {
        
        read();
        String str = System.getProperty(PrpNamingEtk.STR_PRP_PATHABSLASTPARENTFOLDER);
        if (str == null){
            return null;
        }
        str = str.trim();

        if (str.length() < 3){ // !!!!
            return null;
        }
        return str;
    }

    static public void s_setPathAbsLastParentFolder(String str) throws Exception {
        if (str == null){
            System.setProperty(PrpNamingEtk.STR_PRP_PATHABSLASTPARENTFOLDER, "");
            return;
        }
        str = str.trim();
        if (str.length() < 1){ //
            return;
        }
        str = str.replace('\\', '/');
        System.setProperty(PrpNamingEtk.STR_PRP_PATHABSLASTPARENTFOLDER, str);
        write();
        return;
    }

    protected static void write() throws Exception{
        File fleFolderVersion = PrpMgrAbs.s_getOrCreateVersionDir();
        String strPathFile =
                fleFolderVersion.toString() +
                java.io.File.separator +
                PrpMgrAppli._STR_NAMEFILE_ +
                PrpMgrAbs._STR_FILEEXT_;
        PrpMgrAbs.write(_prp_, strPathFile );
        return;
    }

    protected static void read() throws Exception{
        File fleFolderVersion = PrpMgrAbs.s_getOrCreateVersionDir();
        String strPathFile =
                fleFolderVersion.toString() +
                java.io.File.separator +
                PrpMgrAppli._STR_NAMEFILE_ +
                PrpMgrAbs._STR_FILEEXT_;
        PrpMgrAbs.read(_prp_, strPathFile);
        return;
    }

    private static PrpMgrAppli _INSTANCE_;

    final static private String _STR_NAMEFILE_ = "myapp";
    static private Properties _prp_ = new Properties();
    final static public String STR_CONFIG_TRUE = "true";
    final static public String STR_CONFIG_FALSE = "false";


}
